#include "FormBaseWidget.h"
#include "components/StyleSheetSet.h"
#include "components/CommonFunc.h"
#include "tools/cJSON.h"

// 标签页基类初始化
BaseFormWidget::BaseFormWidget(QWidget * parent)
    : QWidget(parent)
    , m_eProcType(FORM_NULL)
    , m_uiFormIndex(0)
    , m_pQVBoxLayout(nullptr)
    , m_pBaseFunc(nullptr)
{
    m_vecFunc.clear();
    m_strSavePath.clear();

    m_pQVBoxLayout = new QVBoxLayout(this);
    m_pQVBoxLayout->setSpacing(1);                  // 默认设置
    m_pQVBoxLayout->setContentsMargins(5, 0, 5, 5); // 设置上下左右的间距

    m_pBaseFunc = new QHBoxLayout;
    m_pQVBoxLayout->addLayout(m_pBaseFunc);
    m_pBaseFunc->setContentsMargins(0, 0, 0, 0);    // 工具栏设置无间距
    m_pBaseFunc->setSpacing(0);
}

void BaseFormWidget::newBaseClose()
{
    QPushButton  *pClose = new QPushButton;
    connect(pClose, SIGNAL(clicked()), this, SLOT(sltCloseTab()));

    //pClose->setText(QStringLiteral("关闭"));
    pClose->setFlat(true);
    pClose->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/close.ico")));
    pClose->setToolTip(QStringLiteral("关闭本标签页"));
    pClose->setStatusTip("关闭本标签页");
    m_pBaseFunc->addWidget(pClose);
    m_vecFunc.push_back(pClose);
}

void BaseFormWidget::sltCloseTab()
{
    if(true == AnaConfig::Instance().getCloseCheck())
    {
        QMessageBox::StandardButton box;
        box = QMessageBox::question(this, QStringLiteral("提示"), QStringLiteral("是否关闭本标签页"), QMessageBox::Yes | QMessageBox::No);
        if(box == QMessageBox::Yes)
        {
            emit SendClose(m_uiFormIndex);
        }
    }
    else
    {
        emit SendClose(m_uiFormIndex);
    }
}

void BaseFormWidget::setFormIndex(uint iKey, FORM_PROC_TYPE eProcType)
{
    m_uiFormIndex = iKey;
    m_eProcType = eProcType;
}

void BaseFormWidget::setBaseFont(const QFont &stFont)
{
    for(auto &it: qAsConst(m_vecFunc))
    {
        it->setFont(stFont);
    }
}

void BaseFormWidget::addBaseToolFunc(QWidget *pFunc, bool bAddSpace)
{
    m_pBaseFunc->addWidget(pFunc);
    m_vecFunc.push_back(pFunc);
    if(true == bAddSpace)
    {
        m_pBaseFunc->addSpacing(FUNC_PUSHBUTTON_SPACE);
    }
}

void BaseFormWidget::addBaseFuncStretch()
{
    m_pBaseFunc->addStretch();
}

void BaseFormWidget::addFunc2Vec(QPushButton *pFunc)
{
    m_vecFunc.push_back(pFunc);
}

// 主界面添加控件
void BaseFormWidget::addWidget2BaseLayout(QWidget *pWidget, Qt::Alignment alignment)
{
    m_pQVBoxLayout->addWidget(pWidget, alignment);
}

// 主界面添加栅栏控件
void BaseFormWidget::addLayout2BaseLayout(QLayout *layout)
{
    m_pQVBoxLayout->addLayout(layout);
}

// 设置主栅栏间隔
void BaseFormWidget::setMainLayoutSpacing(uint ival)
{
    m_pQVBoxLayout->setSpacing(ival);
}
